package com.winit.distributed.lock.autoconfig;

import com.winit.distributed.lock.DistributedLock;
import com.winit.distributed.lock.RedisDistributedLock;
import org.redisson.api.RedissonClient;
import org.redisson.spring.starter.RedissonAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author liuming.li
 */
@Configuration
@ConditionalOnBean(RedissonClient.class)
@EnableConfigurationProperties(DistributedLockProperties.class)
@Import(RedissonAutoConfiguration.class)
public class DistributedLockAutoConfiguration {

    @Autowired
    private DistributedLockProperties distributedLockProperties;


    @Bean
    @ConditionalOnMissingBean(DistributedLock.class)
    public DistributedLock distributedLock(RedissonClient redissonClient) {
        return new RedisDistributedLock(redissonClient, distributedLockProperties.getLockPrefix());
    }
}
